/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function Comprobar(obj) {
    if(obj.toString()!=="[object HTMLFormElement]"){
        alert("El objeto no es un formulario");
        return false;
    }
    
    input = obj.getElementsByTagName("input");
    radio = false;
    checkbox = false;
    j = -1;
    k=-1;
    for(i=0, len=input.length; i<len; i++){
        if(input[i].type == "text"){
            if(!validarText(input[i])){
                return false;
            }
            continue;
        }
        if(input[i].type == "textarea"){
            if(!validarTextArea(input[i])){
                return false;
            }
            continue;
        }
        if(input[i].type == "password"){
            if(!validarPassword(input[i])){
                return false;
            }
            continue;
        }
        if(input[i].type == "radio"){
            j=i;
            if(validarRadio(input[i])){
                radio = true;
            }
        }
        if(input[i].type == "checkbox"){
            k=i;
            if(validarRadio(input[i])){
                checkbox = true;
            }
        }
    }
    if(!radio && j>-1){
        alert(input[j].name + ' incorrecto.!');
        return false;
    }
    if(!checkbox && k>-1){
        alert(input[k].name + ' incorrecto.!');
        return false;
    }
    return true;
}

function validarText(campo){
    var RegExPattern = /[aA-zZ]{3,}/; 
    var errorMessage = campo.name +' Incorrecto.';
    if ((campo.value.match(RegExPattern)) && (campo.value!='')) {
        return true; 
    } else {
        alert(errorMessage);
        campo.focus();
        return false;
    } 
}

function validarTextArea(campo){
    var RegExPattern = /[\w\W]$/;
    var errorMessage = campo.name + ' Incorrecto.';
    if ((campo.value.match(RegExPattern)) && (campo.value!='')) {
        return true; 
    } else {
        alert(errorMessage);
        campo.focus();
        return false;
    } 
}

function validarPassword(campo){
    var RegExPattern = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/;
    var errorMessage = campo.name + ' Incorrecto.';
    if ((campo.value.match(RegExPattern)) && (campo.value!='')) {
        return true; 
    } else {
        alert(errorMessage);
        campo.focus();
        return false;
    } 
}

function validarRadio(campo){
    if(campo.checked){
        return true;
    }
    return false;
}